import axios from "axios";
import { ElMessage } from "element-plus";

// 配置新建一个 axios 实例
const service = axios.create({
  baseURL: "/api",
  timeout: 50000,
});

// 添加请求拦截器
service.interceptors.request.use((config: any) => {
  return config;
});

// 添加响应拦截器
service.interceptors.response.use(
  // async (response: AxiosResponse<ResponseData<any>>) => {
  async (response) => {
    if ((await response).status === 200) {
      if (response.headers["x-total-count"]) {
        return await response;
      }
      return (await response).data;
    } else if ((await response).status === 201) {
      return await response;
    }
  },
  (error: any) => {
    ElMessage.error("请求出错");
    return Promise.reject(error);
  }
);

export default service;
